package com.xue.controller;

import com.xue.pojo.Emp;
import com.xue.pojo.EmpQueryParam;
import com.xue.pojo.PageResult;
import com.xue.pojo.Result;
import com.xue.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;


    /*
     * 带条件的分页查询
     * 和
     * 普通的分页查询
     * */
    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
        log.info("查询员工信息, page={}, pageSize={}");
        PageResult pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 添加员工
     */
    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("请求参数emp: {}", emp);
        empService.save(emp);
        return Result.success();
    }

    /**
     * 批量删除员工
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("批量删除部门: ids={} ", Arrays.asList(ids));//将数组转为list
        empService.delete(ids);
        return Result.success();
    }


    /*
     * 根据id查询员工基本信息
     * 员工工作经历
     * */

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询员工信息, id: {}", id);
        /*Emp emp = empService.getById(id);*/
        Emp emp = empService.getInfo(id);
        return Result.success(emp);
    }

    /*
    * 更新员工信息
    * */
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("更新员工信息: {}", emp);
        empService.update(emp);
        return Result.success();
    }


    /*
    * 查询全部员工
    * */

    @GetMapping("/list")
    public Result list() {
        log.info("查询全部员工");
        List<Emp> empList = empService.list();
        return Result.success(empList);
    }
}
